package com.hspider;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class Main {
	public static void fileC(String filename,InputStream input) throws IOException{
		OutputStream output = new FileOutputStream(filename);
		int tempByte = -1;
		byte[] buf = new byte[1024]; 
		while ((tempByte = input.read(buf)) > 0) {
			output.write(buf, 0, tempByte);
		}
		// 关闭输入输出流
		if (input != null) {
			input.close();
		}
		if (output != null) {
			output.close();
		}
	}
	public static void fun1(String url) throws Exception{
		DefaultHttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); // 设置请求超时时间
		httpclient.getParams().setParameter(
				CoreConnectionPNames.SO_TIMEOUT, 30000); // 读取超时
		
		HttpContext localContext = new BasicHttpContext();
		HttpGet httpget = new HttpGet(url);
		HttpResponse response = httpclient.execute(httpget, localContext);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			HttpHost target = (HttpHost) localContext.getAttribute(	ExecutionContext.HTTP_TARGET_HOST);
			System.out.println("Final target: " + target);
			HttpEntity entity = response.getEntity();
			String filename = "E://hjk//a.gif";
			Main.fileC(filename, entity.getContent());
			EntityUtils.consume(entity);
		}
	}
	
	public static void fun2(String url) throws Exception{
		HttpClient httpclient = new DefaultHttpClient();
		
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("wd", "韩建凯"));
		URI uri = URIUtils.createURI("http", "www.baidu.com", -1, "/s",
				URLEncodedUtils.format(formparams, "UTF-8"), null);
		
		HttpGet httpget = new HttpGet(uri);
		HttpResponse response = httpclient.execute(httpget);
		if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
			String filename = "E://hjk//a.txt";
			Main.fileC(filename, response.getEntity().getContent());
		}
		
		
	}
	
	
	public static void main(String[] args) throws Exception {
		Main.fun1("http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif");
		Main.fun2("http://www.baidu.com/s");
	}

}
